export const findMenuItems = (menu, keys) => {
  let result = [];

  for (const item of menu) {
    if (keys.includes(item.key)) {
      result.push(item);
    }

    if (item.children) {
      result = result.concat(findMenuItems(item.children, keys));
    }
  }

  return result;
};

export const formatNumber = (value: string) => {
  value += "";
  const list = value.split(".");
  const prefix = list[0].charAt(0) === "-" ? "-" : "";
  let num = prefix ? list[0].slice(1) : list[0];
  let result = "";

  while (num.length > 3) {
    result = `,${num.slice(-3)}${result}`;
    num = num.slice(0, num.length - 3);
  }

  if (num) {
    result = num + result;
  }

  return `${prefix}${result}${list[1] ? `.${list[1]}` : ""}`;
};

export const inputFormatNum = (val: string, preVal: string) => {
  const reg = /^-?\d*(\.\d*)?$/;

  if ((!isNaN(+val) && reg.test(val)) || val === "" || val === "-") {
    return val;
  } else {
    return preVal;
  }
};
